package com.codename1.samples;

import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.ui.Button;
import com.codename1.ui.FontImage;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename
 * One</a> for the purpose of building native mobile applications using Java.
 * 
 * Test case for https://github.com/codenameone/CodenameOne/issues/2966
 */
public class AndroidLineAboveTitleTest2966 {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if (err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });
    }

    public void start() {
        if (current != null) {
            current.show();
            return;
        }
        new BugForm1().show();
    }

    public void stop() {
        current = getCurrentForm();
        if (current instanceof Dialog) {
            ((Dialog) current).dispose();
            current = getCurrentForm();
        }
    }

    public void destroy() {
    }

    public static class BugForm1 extends Form {

        BugForm1() {
            super(BoxLayout.y());

            final Toolbar toolbar = new Toolbar(false);
            setToolbar(toolbar);
            toolbar.setUIID("Toolbar");
            toolbar.addMaterialCommandToSideMenu("Mandanten", FontImage.MATERIAL_3D_ROTATION, e -> new BugForm2().show());

            setTitle("BugForm1");

            final Button button = new Button("switch");
            button.addActionListener(a -> new BugForm2().show());
            add(button);

            revalidate();
        }
    }

    public static class BugForm2 extends Form {

        BugForm2() {
            super(BoxLayout.y());

            setTitle("BugForm2");

            final Button button = new Button("switch");
            button.addActionListener(a -> new BugForm1().show());
            add(button);

            revalidate();
        }
    }

}
